﻿// SMIL is a community project available under the Microsoft Public License (Ms-PL)
// Code is provided as is and with no warrenty – Use at your own risk
// View the project and the latest code at http://smil.codeplex.com/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Browser;
using System.Xml.Linq;

namespace SMIL.Client.WPXAP.InfoTool
{
    public class InfoToolParams
    {
        public string Title { get; set; }
        public string Subtitle { get; set; }
        public string IconUrl { get; set; }
        public string ObjectUrl { get; set; }
        public Guid Guid { get; set; }
        public string ViewName { get; set; }
        public string[] ViewNames { get; set; }
        public string ListName { get; set; }
        public string SPSiteURI { get; set; }
        public string SPItemID { get; set; }
    }

    public partial class InfoTool : UserControl
    {        
        SPCommunication.ASMX.SPGeoRssSISoapClient client;
        InfoToolParams _i;

        public InfoTool(InfoToolParams i)
        {
            InitializeComponent();

            _i = i;            
            client = SPCommunication.Proxy.GetProxy;
            client.GetSPItemDetailCompleted += new EventHandler<SMIL.Client.WPXAP.SPCommunication.ASMX.GetSPItemDetailCompletedEventArgs>(client_GetSPItemDetailCompleted);
            Loaded += new RoutedEventHandler(InfoTool_Loaded);
        }            

        public class ItemDetail
        {
            public string fieldName { get; set; }
            public string fieldType { get; set; }
            public string fieldValue { get; set; }
        }

        public XElement Details
        {
            set
            {
                var d = (from detail in value.Descendants("detail")
                         select new ItemDetail
                         {
                             fieldName = detail.Attribute("fieldName").Value.ToString(),
                             fieldType = detail.Attribute("type").Value.ToString(),
                             fieldValue = detail.Value.ToString()
                         });

                try
                {
                    ItemDetail id = d.First(param => param.fieldName == "ID");
                    _i.SPItemID = id.fieldValue;
                    PART_Selectbutton.Visibility = Visibility.Visible;
                }
                catch
                {
                    PART_Selectbutton.Visibility = Visibility.Collapsed;
                }

                PART_Details.ItemsSource = d;
            }
        }

        void InfoTool_Loaded(object sender, RoutedEventArgs e)
        {
            PART_Title.Text = _i.Title;
            PART_Subtitle.Text = _i.Subtitle;
            PART_Views.ItemsSource = _i.ViewNames;
            PART_Views.SelectedItem = _i.ViewName;           
        }

        private void PART_Views_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            _i.ViewName = PART_Views.SelectedItem as string;
            GetDetails();
        }               

        public void GetDetails()
        {            
            client.GetSPItemDetailAsync(new SMIL.Client.WPXAP.SPCommunication.ASMX.ItemDetailRequest()
            {
                SiteUrl = _i.SPSiteURI,
                SPItemGuid = _i.Guid,
                SPListname = _i.ListName,
                SPViewName = _i.ViewName
            });
        }

        void client_GetSPItemDetailCompleted(object sender, SMIL.Client.WPXAP.SPCommunication.ASMX.GetSPItemDetailCompletedEventArgs e)
        {
            Details = e.Result.SPItemDetail;
        }

        private void PART_Openbutton_Click(object sender, RoutedEventArgs e)        
        {
            HtmlPage.Window.Navigate(new Uri(_i.ObjectUrl), "_blank");
        }

        private void PART_Selectbutton_Click(object sender, RoutedEventArgs e)        
        {
            try
            {
                HtmlPage.Window.Invoke("ConnectScript", new string[] { _i.SPItemID });
            }
            catch (Exception ex)
            {
                string msg = string.Empty;
                msg = ex.Message;
                while (ex.InnerException != null)
                {
                    msg += "... Innerexception: " + ex.Message;
                    ex = ex.InnerException;
                }

                MessageBox.Show("Någonting fel vid anrop till Web Part Javascript. Meddelande: " + msg);
            }
        }        
    }
}
